package com.material.event.listener;

import com.material.event.entity.PoundSettleBatchEvent;
import com.material.event.entity.PoundSettleEvent;
import com.material.pound.service.IFuelPoundService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author ganxinge
 */
@Component
public class PoundSettleListener {


    private static final Logger logger = LoggerFactory.getLogger(PoundSettleListener.class);

    @Autowired
    private IFuelPoundService fuelPoundService;

    @EventListener
    @Async
    public void handlerPoundSettleEvent(PoundSettleEvent event){
        try {
            Long id = event.getId();
            logger.info("异步事件监听开始:{}",id);
            fuelPoundService.updateSettleInfo(id);
            logger.info("异步事件监听结束");
        }catch (Exception e){
            logger.error("生成结算信息异常",e);
        }
    }

    @EventListener
    @Async
    public void handlerPoundSettleBatchEvent(PoundSettleBatchEvent event){
        try {
            List<Long> ids = event.getIds();
            logger.info("异步事件监听开始:{}",ids);
            for (int i = 0; i < ids.size(); i++) {
                Long id = ids.get(i);
                fuelPoundService.updateSettleInfo(id);
            }
            logger.info("异步事件监听结束");
        }catch (Exception e){
            logger.error("生成结算信息异常",e);
        }
    }
}
